//
//  SLRefreshBoatViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/5.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLRefreshBoatViewController.h"
#import "SLPackageOrBoatTypeViewController.h"
#import "SLDataPickViewController.h"
#import "STPopup.h"
#import "SLLocatinViewController.h"
#import "DateTools.h"
#import "SLRefreshBoatViewModel.h"
#import "DateTools.h"


@interface SLRefreshBoatViewController (){
  UIImageView *_imageView;
  UILabel *_titleLB;
  UILabel *_statusLB;
}

@property (nonatomic, strong) SLDeployShip *shipModel;

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UILabel *secondLabel;

@property (nonatomic, strong) UITextField *secondeTF;

@property (nonatomic, strong) UITextField *statusTF;

@property (nonatomic, strong) UITextField *locationTF;

@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *dataPickBtn;

@property (nonatomic, strong) UIButton *locationBtn;

@property (nonatomic, strong) NSMutableDictionary *jsonDic;

@property (nonatomic, assign) NSInteger status;

@property (nonatomic, strong) QMSPoiData *locationData;

@property (nonatomic, strong) SLRefreshBoatViewModel *refreshVM;

@property (nonatomic, strong) UILabel *locationLB;

@end



@implementation SLRefreshBoatViewController

- (void)dealloc{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (instancetype)initWithState:(SLDeployShip *)shipModel{
  if (self = [super init]) {
    self.shipModel = shipModel;
    self.view.backgroundColor = [UIColor clearColor];
    }
  return self;
}


- (void)viewDidLoad{
  [super viewDidLoad];
  [self setUpUI];
  [self addConstrians];
  [self configWithState:self.shipModel.state];

  
  @weakify(self)
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"changeLocation" object:nil] subscribeNext:^(NSNotification *notify) {
    @strongify(self)
    QMSPoiData *data = notify.userInfo[@"location"];
    self.locationData = data;
    self.locationTF.text = data.title;
  }];
  
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


#pragma mark - Private Methods

- (void)configWithState:(NSInteger)state{
  self.status = state;
  [self setStatusWithState:state];
  [self.contentView layoutIfNeeded];
}

- (void)setStatusWithState:(NSInteger)state{
  
  self.secondeTF.enabled = YES;
  self.locationLB.hidden = NO;
  self.secondeTF.keyboardType = UIKeyboardTypeDefault;
  [self.secondeTF bringSubviewToFront:self.dataPickBtn];
  self.dataPickBtn.hidden = YES;
  
  if (state == 0) {
    self.statusTF.text = @"满载";
    NSString *text = @"需要寻货时,请及时更新状态";
    NSString *tftext = @"祝一路顺风";
    NSAttributedString *textLB = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName :[UIColor redColor]}];
    NSAttributedString *textTF = [[NSAttributedString alloc] initWithString:tftext attributes:@{NSForegroundColorAttributeName :[UIColor redColor]}];
    self.secondLabel.attributedText = textLB;
    self.secondeTF.attributedText = textTF;
    self.dataPickBtn.hidden = YES;
    self.locationLB.hidden = YES;
    self.locationTF.hidden = YES;
    [self stausFull];

  }else{
    self.secondeTF.text = @"";
    self.locationTF.hidden = NO;
    if (state == 1) {
      //空船找货
      self.statusTF.text = @"空船找货";
      self.secondLabel.text = @"     ";
      self.secondeTF.text = @"   ";
      self.secondeTF.enabled = NO;
      self.dataPickBtn.hidden = YES;
      self.locationLB.text = @"当前位置";
    }
    
    if (state == 2) {
      //半船找货
      self.statusTF.text = @"半船找货";
      self.secondLabel.text = @"空余吨位";
      self.secondeTF.placeholder = @"填写空余吨位";
      self.locationLB.text = @"当前位置";
      self.secondeTF.keyboardType = UIKeyboardTypeNumberPad;
      self.dataPickBtn.hidden = YES;
    }
    
    if (state == 3) {
      //预定货物
      self.statusTF.text = @"预定货物";
      self.secondLabel.text = @"空船时间";
      self.secondeTF.placeholder = @"填写空船时间";
      self.locationLB.text = @"预定位置";
      self.dataPickBtn.hidden = NO;
      [self.view endEditing:YES];
    }
    [self.locationTF mas_remakeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(self.secondeTF);
      make.right.equalTo(self.secondeTF);
      make.top.equalTo(self.locationLB.mas_bottom).offset(15);
      make.height.equalTo(@53);
    }];
    
    [self.locationBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
      make.right.equalTo(self.locationTF).offset(-2.5f);
      make.height.equalTo(self.locationTF);
      make.centerY.equalTo(self.locationTF);
      make.width.equalTo(@25);
    }];

    
    [self statusNotFull];
  }
  
}



- (void)showStatusPicker{
  SLPackageOrBoatTypeViewController *picker = [[SLPackageOrBoatTypeViewController alloc] initWithType:PackageOrBoatTypeBoatState];
  STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:picker];
  popVC.cornerRadius = 5;
  popVC.navigationBarHidden = YES;
  [popVC presentInViewController:self];
  
  @weakify(self)
  [picker returnReturnPackageOrBoatType:^(NSString *type) {
    @strongify(self)
    self.status = type.integerValue;
    [self configWithState:type.integerValue];
  }];
}

- (void)showDatePicker{
  SLDataPickViewController *picker = [[SLDataPickViewController alloc] init];
  STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:picker];
  popVC.navigationBarHidden = YES;
  popVC.cornerRadius = 5.f;
  [popVC presentInViewController:self];
  
  @weakify(self)
  [picker returnDate:^(NSString *date) {
    @strongify(self)
    self.secondeTF.text = date;
  }];
}

- (void)skipToMapView{
  SLLocatinViewController *locationVC = [SLLocatinViewController new];
  [self.presentingViewController.navigationController pushViewController:locationVC animated:YES];
}


- (void)cancelAction{
  [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)confirmAction{
  
  if (self.status == 0) {
      [self changeToServiceWithStatus:self.status];
  }
  
  if (self.status == 1) {
    if (self.locationTF.text.length > 0) {
      [self changeToServiceWithStatus:self.status];
    }else{
      [SVProgressHUD showImage:nil status:@"请选择位置"];
    }
  }
  
  if (self.status == 2 || self.status == 3) {
    if (self.secondeTF.text.length > 0 && self.locationTF.text.length > 0) {
      [self changeToServiceWithStatus:self.status];
    }else{
      [SVProgressHUD showImage:nil status:@"请完善信息"];
    }
  }
  
  
}

-(void)changeToServiceWithStatus:(NSInteger)state{
  
  [self.jsonDic removeAllObjects];
  [self.jsonDic setObject:@(state) forKey:@"state"];
  [self.jsonDic setObject:self.shipModel.bid forKey:@"bid"];
  [self.jsonDic setObject:@(self.shipModel.shiplat) forKey:@"shiplat"];
  [self.jsonDic setObject:@(self.shipModel.shiplon) forKey:@"shiplon"];
  [self.jsonDic setObject:self.locationTF.text forKey:@"shiploc"];
  
  if (state == 1) {
    //空船找货
    if (self.locationData != nil) {
      [self.jsonDic setObject:@(self.locationData.location.latitude) forKey:@"shiplat"];
      [self.jsonDic setObject:@(self.locationData.location.longitude) forKey:@"shiplon"];
    }else{
      [self.jsonDic setObject:@0 forKey:@"shiplat"];
      [self.jsonDic setObject:@0 forKey:@"shiplon"];
    }
  }
  
  if (state == 2) {
    //半船找货
    if (self.secondeTF.text.integerValue > self.shipModel.loads) {
      [SVProgressHUD showImage:nil status:@"空余吨位必须小于船舶载重"];
      return;
    }
    [self.jsonDic setObject:self.secondeTF.text forKey:@"freespace"];
  }
  
  if (state == 3) {
    //预定货物
    if (self.locationData != nil) {
      [self.jsonDic setObject:@(self.locationData.location.latitude) forKey:@"goallat"];
      [self.jsonDic setObject:@(self.locationData.location.longitude) forKey:@"goallon"];
    }else{
      [self.jsonDic setObject:@0 forKey:@"goallat"];
      [self.jsonDic setObject:@0 forKey:@"goallon"];
    }    
    [self.jsonDic setObject:self.secondeTF.text forKey:@"reservetime"];
    [self.jsonDic setObject:self.locationTF.text forKey:@"goalloc"];
  }

  @weakify(self)
  [self.refreshVM refreshBoatWithStateJson:self.jsonDic block:^{
    @strongify(self)
    [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshBoatSuccess object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
  }];

}


- (void)keyBoardWillShow:(NSNotification *)notify{
    
  [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(30*MIX_H);
  }];
}

- (void)keyBoardWillHide:(NSNotification *)notify{
  [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(100*MIX_H);
  }];
}



- (void)setUpUI{
  _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"boat1"]];
  _titleLB = [UILabel labelWithFontSize:16 textColor:[UIColor blackColor] text:self.shipModel.boatname];
  _statusLB = [UILabel labelWithFontSize:15 textColor:[UIColor lightGrayColor] text:@"当前状态"];
  
  UIView *backView = [[UIView alloc] initWithFrame:self.view.bounds];
  backView.backgroundColor = [UIColor grayColor];
  backView.alpha = 0.6;
  [self.view addSubview:backView];
  [self.view addSubview:self.contentView];
  [self.contentView addSubview:_imageView];
  [self.contentView addSubview:_titleLB];
  [self.contentView addSubview:_statusLB];
  [self.contentView addSubview:self.locationTF];
  [self.contentView addSubview:self.locationLB];
  [self.locationTF addSubview:self.locationBtn];
  [self.contentView addSubview:self.statusTF];
  [self.contentView addSubview:self.secondLabel];
  [self.contentView addSubview:self.secondeTF];
  [self.secondeTF addSubview:self.dataPickBtn];
  [self.contentView addSubview:self.cancelBtn];
  [self.contentView addSubview:self.confirmBtn];
}


- (void)addConstrians{
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(100*MIX_H);
    make.width.equalTo(self.view).offset(-60);
    make.centerX.equalTo(self.view);
  }];
  
  [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.top.equalTo(self.contentView).offset(10);
    make.size.mas_equalTo(CGSizeMake(25, 25));
  }];
  
   [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(_imageView);
    make.centerX.equalTo(self.contentView);
  }];
  
  [_statusLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_imageView);
    make.top.equalTo(self.contentView).offset(45);
  }];

  [self.statusTF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_statusLB);
    make.right.equalTo(self.contentView).offset(-5);
    make.top.equalTo(self.contentView).offset(70);
    make.height.equalTo(@21);
  }];
  
  UIButton *statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  [statusBtn addTarget:self action:@selector(showStatusPicker) forControlEvents:UIControlEventTouchUpInside];
  [self.contentView addSubview:statusBtn];
  [statusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.statusTF);
  }];
  
  [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_imageView);
    make.top.equalTo(self.statusTF.mas_bottom).offset(10);
  }];

  [self.secondeTF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_imageView);
    make.right.equalTo(self.contentView).offset(-5);
    make.top.equalTo(self.secondLabel.mas_bottom).offset(15);
    make.height.equalTo(@22);
  }];

  [self.dataPickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.secondeTF);
  }];
  
  [self.locationLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_statusLB);
    make.top.equalTo(self.secondeTF.mas_bottom).offset(15);
  }];
  
  [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(_imageView);
    make.height.equalTo(@40);
    make.width.equalTo(@100);
  }];
  
  [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.contentView).offset(-10);
    make.width.equalTo(@100);
    make.top.height.equalTo(self.confirmBtn);
  }];
  
  [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.confirmBtn).offset(10);
  }];

}

- (void)stausFull{
  [self.confirmBtn mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.secondeTF.mas_bottom).offset(40);
  }];
}

- (void)statusNotFull{
  [self.confirmBtn mas_updateConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.secondeTF.mas_bottom).offset(110);
  }];
}


#pragma mark - getter

-(SLRefreshBoatViewModel *)refreshVM{
  if (!_refreshVM) {
    _refreshVM = [SLRefreshBoatViewModel new];
  }
  return _refreshVM;
}

- (UIButton *)locationBtn{
  if (!_locationBtn) {
    _locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_locationBtn setImage:[UIImage imageNamed:@"map"] forState:UIControlStateNormal];
    [_locationBtn addTarget:self action:@selector(skipToMapView) forControlEvents:UIControlEventTouchUpInside];
  }
  return _locationBtn;
}

- (NSMutableDictionary *)jsonDic{
  if (!_jsonDic) {
    _jsonDic = [[NSMutableDictionary alloc] initWithCapacity:1];
  }
  return _jsonDic;
}

- (UIView *)contentView{
  if (!_contentView) {
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.layer.cornerRadius = 5.f;
  }
  return _contentView;
}

- (UITextField *)statusTF{
  if (!_statusTF) {
    _statusTF = [[UITextField alloc] init];
    _statusTF.rightViewMode = UITextFieldViewModeAlways;
    UIImageView *rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"v"]];
//    rightView.transform = CGAffineTransformRotate(rightView.transform, M_PI_4);
    _statusTF.rightView = rightView;
  }
  return _statusTF;
}

- (UITextField *)locationTF{
  if (!_locationTF) {
    _locationTF = [[UITextField alloc] init];
    _locationTF.layer.cornerRadius = 4;
    _locationTF.rightViewMode = UITextFieldViewModeAlways;
    _locationTF.backgroundColor = [UIColor lightGrayColor];
    
  }
  return _locationTF;
}

- (UILabel *)locationLB{
  if (!_locationLB) {
    _locationLB = [UILabel labelWithFontSize:15 textColor:[UIColor lightGrayColor] text:@"地址"];
  }
  return _locationLB;
}

- (UITextField *)secondeTF{
  if (!_secondeTF) {
    _secondeTF = [UITextField new];
    _secondeTF.backgroundColor = [UIColor whiteColor];
  }
  return _secondeTF;
}

- (UILabel *)secondLabel{
  if (!_secondLabel) {
    _secondLabel = [UILabel labelWithFontSize:15 textColor:[UIColor lightGrayColor] text:@"second"];
  }
  return _secondLabel;
}

- (UIButton *)dataPickBtn{
  if (!_dataPickBtn) {
    _dataPickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _dataPickBtn.backgroundColor = [UIColor clearColor];
    [_dataPickBtn addTarget:self action:@selector(showDatePicker) forControlEvents:UIControlEventTouchUpInside];
  }
  return _dataPickBtn;
}

- (UIButton *)cancelBtn{
  if (!_cancelBtn) {
    _cancelBtn = [UIButton buttonWithTitle:@" 取消 "];
    _cancelBtn.backgroundColor = SLYellowColor;
    [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  }
  return _cancelBtn;
}

- (UIButton *)confirmBtn{
  if (!_confirmBtn) {
    _confirmBtn = [UIButton buttonWithTitle:@" 确定 "];
    _confirmBtn.backgroundColor = SLYellowColor;
    [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  }
  return _confirmBtn;
}



@end
